<?php
/**
 * Created by PhpStorm.
 * User: ari
 * Date: 11/20/14
 * Time: 7:47 PM
 */
namespace BC\User\Role;

use BC\PGP\Commands\PGPSearchCommand;
use BC\User\Exceptions;
use BC\User\Session\Exceptions\UserSessionNotFound;
use CPath\Request\IRequest;

class RoleSearch extends PGPSearchCommand
{
	function __construct($roleSearch) {
		parent::__construct($roleSearch);
		$this->setHomeDir(RoleConfig::$RoleKeyRingPath);
	}

	/**
	 * @param \CPath\Request\IRequest $Request
	 * @return RoleSearch
	 */
	function queryFirst(IRequest $Request=null) {
		return $this->queryFirst($Request);
	}

	/**
	 * @param \CPath\Request\IRequest $Request
	 * @throws \BC\User\Session\Exceptions\UserSessionNotFound
	 * @throws \Exception
	 * @return RoleSearch
	 */
	function queryOne(IRequest $Request=null) {
		$All = $this->queryAll($Request);

		if (sizeof($All) <= 0)
			throw new UserSessionNotFound("User session not found: " . $this->getSearchString());

		if (sizeof($All) > 1)
			throw new \Exception("Multiple user sessions found: " . $this->getSearchString());

		return reset($All);
	}

	/**
	 * @param \CPath\Request\IRequest $Request
	 * @return RoleSearch[]
	 */
	function queryAll(IRequest $Request=null) {
		return parent::queryAll($Request);
	}
}